<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 新生管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-input
          v-model="query.name"
          placeholder="请输入学生姓名"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="handleImport"
          >导入</el-button
        > -->
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          label-width="20px"
        >
          <el-form-item label-width="20px">
            <el-select
              v-model="query.collegeId"
              filterable
              placeholder="请选择学院"
              @focus="getAllCollege()"
            >
              <el-option
                v-for="item in options"
                :key="item.sid"
                :label="item.collegeName"
                :value="item.sid"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="query.majorId"
              filterable
              placeholder="请选择专业"
              @focus="getMajor()"
            >
              <el-option
                v-for="item in optionsMjors"
                :key="item.sid"
                :label="item.majorName"
                :value="item.sid"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-input
              v-model="query.studentName"
              placeholder="姓名"
              class="inline-input"
            ></el-input>
          </el-form-item>
          <!--          <el-form-item>-->
          <!--            <el-input-->
          <!--              v-model="query.gender"-->
          <!--              placeholder="年级"-->
          <!--              class="inline-input"-->
          <!--            ></el-input>-->
          <!--          </el-form-item>-->
          <el-form-item>
            <el-input
              v-model="query.domicilePlaceProvince"
              placeholder="省份"
              class="inline-input"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleSearch"
              >搜索</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click="handleAdd"
              >新增</el-button
            >
          </el-form-item>
          <el-form-item>
               <el-button
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click="handleImport"
              >导入</el-button
            >
              <el-button
                type="primary"
                icon="el-icon-circle-plus-outline"
                @click="handleExport"
                >导出(存在班级学生)</el-button
              >
       
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          :show-overflow-tooltip="true"
          :prop="item.prop"
          :label="item.label"
          v-for="item in props"
          :key="item.prop"
          align="center"
        >
        </el-table-column>
        <el-table-column>
          <el-table-column
            label="操作"
            width="180"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.row.sid)"
                >编辑
              </el-button>
              <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.$index, scope.row.sid)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :currentPage="query.pageNum"
          :page-sizes="[10, 20, 40]"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 导入弹出框 -->
    <div>
      <el-dialog
        title="导入"
        v-model="importVisible"
        width="20%"
        @close="handleSearch"
      >
        <div class="plugins-tips">
          学生导入模板<span @click="downTemp"
            ><a href="javascript:;">点击此处</a></span
          >下载
        </div>
        <div>
          <el-row :gutter="20">
            <el-upload
              ref="upload"
              class="upload-demo"
              action="http://localhost:9998/student/importStudentPileInfo"
              :on-success="handleUp"
              :auto-upload="false"
              :limit="1"
              :before-remove="beforeRemove"
              :on-exceed="handleExceed"
            >
              <template #trigger>
                <el-col :span="10">
                  <div>
                    <el-button size="small" type="primary"
                      >请选择文件</el-button
                    >
                  </div>
                </el-col>
              </template>

              <el-col :span="20">
                <div style="display: flex">
                  <el-button
                    style="margin-right: 1px"
                    size="small"
                    type="success"
                    :loading="false"
                    @click="submitUpload"
                    >确认上传</el-button
                  >
                </div>
              </el-col>
              <div class="el-upload__tip">*最多只能选则一个文件*</div>
            </el-upload>
          </el-row>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import request from "../../utils/request";

export default {
  name: "NewMng",
  
  methods: {
    //文件上传
    submitUpload() {
      this.$refs.upload.submit();
    },
    //文件数量控制
    handleExceed(files, fileList) {
      this.$message.warning(
        `The limit is 1, you selected ${
          files.length
        } files this time, add up to ${files.length + fileList.length} totally`
      );
    },
    handleUp(response, file, fileList) {
      if (response.code === null) {
        ElMessage.success("导入成功");
        this.importVisible = false;
        this.$refs.upload.clearFiles();
        this.getData();
      } else {
        ElMessageBox.alert(response.msg, "导入失败", {
          confirmButtonText: "确定",
        });
        this.importVisible = false;
        this.$refs.upload.clearFiles();
      }
    },
  },
  setup() {
    const downTemp = () => {
      window.location.href = "http://localhost:9998/files/template";
    };
    const router = useRouter();

    const props = [
      { label: "学生姓名", prop: "studentName" },
      { label: "所属学院", prop: "collegeName" },
      { label: "所属专业", prop: "majorName" },
      { label: "身份证号", prop: "idCardNo" },
      // { label: "学号", prop: "studentId" },
      { label: "手机号", prop: "mobilePhone" },
      {label: "性别", prop: "gender"},
      //   { label: "出生日期", prop: "birth" },
      {label: "民族", prop: "nation"},
      {label: "政治面貌", prop: "political"},
      //  { label: "婚姻状况", prop: "marital" },
      {label: "籍贯（省）", prop: "domicilePlaceProvince"},
      {label: "籍贯（市）", prop: "domicilePlaceCity"},
      {label: "户籍地址", prop: "domicilePlaceAddress"},
      {label: "居住地址", prop: "presentAddress"},
      {label: "电子邮件", prop: "email"},
      // { label: "入学日期", prop: "entryDate" },
      // { label: "年级", prop: "grade" },
    ];

    const query = reactive({
      collegeId: "",
      majorId: "",
      studentName: "",
      grade: "",
      domicilePlaceProvince: "",
      pageNum: 1,
      pageSize: 10,
    });
    const tableData = ref([]);
    const pageTotal = ref(0);
    // 获取表格数据
    const getData = () => {
      request
        .get("/student/page", {
          params: query,
        })
        .then((res) => {
          if (res.code != null) {
            ElMessage.warning(res.msg);
            tableData.value = [];
          } else {
            tableData.value = res.data.records;
            pageTotal.value = res.data.total;
            query.collegeId = "";
            query.majorId = "";
            query.studentName = "";
            query.grade = "";
            query.domicilePlaceProvince = "";
          }
        });
    };
    getData();
    //学院下拉框
    const options = ref([]);
    const getAllCollege = () => {
      console.log(query.collegeId);
      request.get("/college/getAllCollege").then((res) => {
        options.value = res.data;
        getMajor;
      });
    };
    //专业下拉框

    const optionsMjors = ref([]);
    const getMajor = () => {
      request
        .get("/major/queryList", {
          params: { collegeId: query.collegeId, type: 1 },
        })
        .then((res) => {
          optionsMjors.value = res.data;
        });
    };

    // 查询操作
    const handleSearch = () => {
      query.pageNum = 1;
      getData();
    };
    // 分页大小
    const handleSizeChange = (val) => {
      query.pageSize = val;
      getData();
    };
    // 分页导航
    const handlePageChange = (val) => {
      query.pageNum = val;
      getData();
    };

    // 删除操作
    const handleDelete = (index, sid) => {
      // 二次确认删除
      ElMessageBox.confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          request.delete("/student/" + sid).then((res) => {
            if (res.data) {
              ElMessage.success("删除成功");
              tableData.value.splice(index, 1);
            } else {
              ElMessage.error("删除失败");
            }
          });
        })
        .catch(() => {});
    };

    // 新增操作
    const handleAdd = () => {
      router.push("/admin/addNew");
    };

    const handleEdit = (sid) => {
      router.push({ path: "/admin/addNew", query: { id: sid } });
    };

    const importVisible = ref(false);

    const handleImport = () => {
      importVisible.value = true;
    };
    const handleExport = () => {
      window.location.href = "http://localhost:9998/student/export";
    };

    //文件移除
    const beforeRemove = (file, fileList) => {
      ElMessageBox.confirm(`确定移除当前文件的上传 ${file.name} ?`);
    };
    return {
      props,
      query,
      tableData,
      pageTotal,
      importVisible,
      options,
      optionsMjors,
      getAllCollege,
      getMajor,
      handleSearch,
      handleSizeChange,
      handlePageChange,
      handleAdd,
      handleDelete,
      downTemp,
      handleEdit,
      handleImport,
      beforeRemove,
      handleExport,
    };
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}
.exportStudent{
   display: inline-flex;
}
.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
